package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
import org.json.JSONException;
/**
 *
 * @Title: Bp_Get_ZhswToKen
 * @Description: 宝安排水 网格 获取ToKen
 * @author: chieh
 * @date 2020-07-31 12:30:00
 *
 */
public class Bp_Get_ZhswToKen implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(Bp_Get_ZhswToKen.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		log.debug("-------------------------start Bp_Get_ZhswToKen 开始-----------------");

		//获取配置信息
		String URL = getProperty.get("ZHSW.URL")+"api/reception-center/oauth/token";
		String username = getProperty.get("ZHSW.USERNAME");
		String password = getProperty.get("ZHSW.PASSWORD");
		String client_id = getProperty.get("ZHSW.CLIENTID");
		String client_secret = getProperty.get("ZHSW.CLIENTSECRET");
		String grant_type = getProperty.get("ZHSW.GRANTTYPE");
		
		log.debug("URL:" + URL+"	username:" + username+"	password:" + password+"	client_id:" + client_id+"	client_secret:" + client_secret+"	grant_type:" + grant_type);	

		long startTime = System.currentTimeMillis();

		def reJson = "";
		try {
			/*
			username = URLEncoder.encode(username, "utf-8");
			password = URLEncoder.encode(password, "utf-8");
			client_id = URLEncoder.encode(client_id, "utf-8");
			client_secret = URLEncoder.encode(client_secret, "utf-8");
			grant_type = URLEncoder.encode(grant_type, "utf-8");
			*/
			
			String json ='username='+username+'&password='+password+'&client_id='+client_id+'&client_secret='+client_secret+'&grant_type='+grant_type;

			log.debug("拼接前的json：" + json);

			json=URL+"?"+json;
			log.debug("geturl:" + json);
			//json="http://10.99.34.66/api/reception-center/oauth/token?username=lqL3laQbL8Mr7Gd4rmW5LA%3D%3D&password=zXhCijv37rirl4u1Yrqb8Q&client_id=2c93809d70418e880170531876520006&client_secret=741839f467fb42fc91aa7866319e2cee&grant_type=password";
			
			// 设置头文件
			Map<String, String> headMap = new HashMap<String, String>();
			headMap.put("Content-Type", "application/json");
			String injson =Util.doHttpPost(json,"","UTF-8",30000,0);

			log.debug("调用返回信息：" + injson.toString());
			JSONObject inObject = new JSONObject(injson);
			String code = GetJV(inObject, "code");
			String access_token = GetJV(inObject, "access_token");
			log.debug("code" + code);
			log.debug("access_token" + access_token);
			if(access_token!=null&&access_token!=""){
				String expires_in = GetJV(inObject, "expires_in");

				log.debug("access_token:	"+access_token);
				log.debug("expires_in:	"+expires_in);
					
				if(expires_in!=null) {
					try {
						eliteBean.update("update SYSC_PARAMETER set PARAMETERVALUE = '"+access_token+"',MODIFIEDDATE=DATEADD(s, "+expires_in+"*1.0/1000, GETDATE()) where PARAMETER_ID = 'ZHSWGT'", "dataSource");
					} catch (Exception e) {
						access_token = null;
					}
				}
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"BP智慧化网格化获取ToKen接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\""+access_token+"\"}";
			}else{
				long endTime = System.currentTimeMillis();
				reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"BP智慧化网格化获取ToKen接口处理失败(毫秒):" + (endTime - startTime) + "\",\"data\":\"\"}";
			}

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"BP智慧化网格化获取ToKen接口处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":{}}";
		}

		return reJson;
	}
	
	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}
}